const { SimpleData, SuccessData } = require('../common/returnDataFormat');
const Topic = require('../model/topic');
const User = require('../model/user');
const Comment = require('../model/comment');
const Types = require('../model/type');
const sequelize = require('../config/sequelize');
const { findTypeById, findTypeByName } = require('./topicController');

// 通过ID查找用户
module.exports.selectUserById = async (userId) => {
  try {
    const result = await User.findOne({
      where: {
        user_id: userId,
      },
      include: [
        {
          model: Topic,
        },
        {
          model: Comment,
          include: [
            {
              model: Comment,
              as: 'Comments',
            },
          ],
        },
      ],
    });
    return result.get({ plain: true });
  } catch (error) {
    return SimpleData(500, '数据库查询失败');
  }
};
// 修改用户信息
module.exports.modifyUser = async (params) => {
  try {
    let user = await this.selectUserById(params[0]);
    if (user) {
      const result = await User.update(
        {
          ...params[1],
        },
        {
          where: {
            user_id: params[0],
          },
        }
      );
      if (result[0]) {
        return SimpleData(200, '修改用户成功');
      }
    }
    return SimpleData(500, '用户不存在');
  } catch (error) {
    return SimpleData(503, '数据库操作错误');
  }
};

// 封停用户
module.exports.banUser = async (userId) => {
  let user = await this.selectUserById(userId);
  if (user) {
    // const result = await pool(SQL.BanUserById, userId)
    const result = await User.update(
      {
        state: 1 - user.state,
      },
      {
        where: {
          user_id: userId,
        },
      }
    );
    if (result[0]) {
      return SimpleData(200, '操作成功');
    }
  }
  return SimpleData(500, '操作失败');
};

//删除用户
module.exports.delUser = async (userId) => {
  let user = await this.selectUserById(userId);
  const t = await sequelize.transaction();
  try {
    if (user) {
      user = user.get({ plain: true });
      user.comments.forEach((item) => {
        item.Comments.forEach(async (replay) => {
          await Comment.destroy({
            where: {
              id: replay.id,
            },
          });
        });
        Comment.destroy({
          where: {
            id: item.id,
          },
        });
      });
      user.topics.forEach(async (item) => {
        await Topic.destroy({
          where: {
            topic_id: item.topic_id,
          },
        });
      });
      const result = await User.destroy({
        where: {
          user_id: userId,
        },
      });
      console.log(result);
      if (result) {
        await t.commit();
        return SimpleData(200, '用户删除成功');
      }
    }
  } catch (error) {
    console.log(error);
    await t.rollback();
    return SimpleData(500, '用户删除失败');
  }
  return SimpleData(500, '用户删除失败');
};

//删除分类
module.exports.deleteType = async ({ id }) => {
  const res = await findTypeById({ id });
  if (res) {
    const result = await Types.destroy({
      where: {
        id,
      },
    });
    if (result) {
      return SuccessData(result);
    } else {
      return SimpleData(500, '修改失败');
    }
  } else {
    return SimpleData(500, '分类不存在');
  }
};
//修改分类
module.exports.modifyType = async ({ name, id }) => {
  const res = await Types.findOne({
    where: {
      id,
    },
  });
  if (res) {
    const result = await Types.update(
      {
        name,
      },
      {
        where: {
          id,
        },
      }
    );
    if (result) {
      return SuccessData(result);
    } else {
      return SimpleData(500, '修改失败');
    }
  } else {
    return SimpleData(500, '分类不存在');
  }
};

//添加分类
module.exports.addType = async (name) => {
  const res = await findTypeByName(name);
  if (res.code == 500) {
    const result = await Types.create({
      name,
    });
    if (result) {
      return SuccessData(result);
    } else {
      return SimpleData(500, '添加失败');
    }
  } else {
    return SimpleData(500, '分类已存在');
  }
};
